package com.example.demo.controller.admin;


import com.example.demo.entity.BookInfo;
import com.example.demo.entity.Store;
import com.example.demo.service.BookInfoService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.List;

@RequestMapping("/admin/book")
@Controller
@RequiresPermissions("book-manage")
public class AdminBookController {


    @Autowired
    private BookInfoService bookInfoService;


    @Value("${image.url.prefix}")
    private String imageUrlPrefix;




    @RequestMapping("/list")

   public String bookList(@RequestParam(defaultValue = "",required = false)String keywords,@RequestParam(defaultValue = "1",required = false) String page, HttpServletRequest request, Model model){

        keywords=keywords.trim();
        HttpSession session=request.getSession();
        Store loginStore=(Store)session.getAttribute("loginStore");

       // PageHelper.startPage(Integer.parseInt(page),10);
        //List<BookInfo> bookInfoList=bookInfoService.findByStoreId(loginStore.getStoreId());

        PageInfo<BookInfo> bookInfoPageInfo=bookInfoService.findByConditions(0,keywords,Integer.parseInt(page),10,loginStore.getStoreId());

        model.addAttribute("bookPageInfo",bookInfoPageInfo);
        model.addAttribute("keywords",keywords);
        return "/admin/book/list";
    }

    @RequestMapping("/toAddition")
    public String toAddition(){
       return "/admin/book/add";
    }

    @RequestMapping("/addition")
    public String addBook(BookInfo bookInfo, String bookDesc, MultipartFile pictureFile,HttpServletRequest request) throws IOException {

        this.uploadPicture(bookInfo,pictureFile,request);
        bookInfoService.insertBook(bookInfo,bookDesc);
        return "forward:/admin/book/list";

    }

    private void uploadPicture(BookInfo bookInfo, MultipartFile pictureFile, HttpServletRequest request) throws IOException {


        if(pictureFile!=null){
            if(pictureFile.getOriginalFilename()!=null){

            String webPath=request.getServletContext().getRealPath("/"+imageUrlPrefix);
            String oldFileName=pictureFile.getOriginalFilename();

            String newFileName= RandomStringUtils.randomAlphanumeric(15)+oldFileName.substring(oldFileName.lastIndexOf("."));
            File file=new File(webPath+File.separator+newFileName);
            pictureFile.transferTo(file);
            bookInfo.setImage_url(imageUrlPrefix+File.separator+newFileName);

            }
        }
    }


    @RequestMapping("/echo")
    public String edit(String bookId,Model model){

        Integer book_id=Integer.parseInt(bookId);
        BookInfo bookInfo=bookInfoService.findByBookId(book_id);
        model.addAttribute("bookInfo",bookInfo);
        return "/admin/book/edit";
    }

    @RequestMapping("/update")
    public String updateBookInfo(BookInfo bookInfo,String bookDesc){

        bookInfoService.updateBookInfo(bookInfo,bookDesc);
        return "forward:/admin/book/list";
    }

    @RequestMapping("/shelf")
    public String adjustShelfStatus(String bookId, String isShelf, String keywords, RedirectAttributes redirectAttributes){

        Integer book_id=Integer.parseInt(bookId);
        Integer is_shelf=Integer.parseInt(isShelf);
        bookInfoService.changeShelfStatus(book_id,is_shelf);
        redirectAttributes.addAttribute("keywords",keywords);
        return "redirect:/admin/book/list";

    }

    @RequestMapping("/deletion")
    public String adjustShelfStatus(String bookId, String keywords, RedirectAttributes redirectAttributes){

        Integer book_id=Integer.parseInt(bookId);

        bookInfoService.deleteBookInfoByID(book_id);
        redirectAttributes.addAttribute("keywords",keywords);
        return "redirect:/admin/book/list";

    }



}
